﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace FastGPT_Admin.Tool
{
    internal class KeyGenerator
    {
        /// <summary>
        /// 生成密钥和向量Key
        /// </summary>
        /// <param name="characters">盐</param>
        /// <param name="keyLength">密钥长度</param>
        /// <returns></returns>
        public static byte[] GenerateKey(string characters, int keyLength)
        {
            //const string characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            char[] chars = characters.ToCharArray();
            byte[] data = new byte[keyLength];
            using (RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider())
            {
                crypto.GetBytes(data);
            }
            StringBuilder result = new StringBuilder(keyLength);
            foreach (byte b in data)
            {
                result.Append(chars[b % (chars.Length)]);
            }
            return Encoding.UTF8.GetBytes(result.ToString());
        }

        /// <summary>
        /// 根据字符串生成指定密钥或向量
        /// </summary>
        /// <param name="keyString">字符串</param>
        /// <param name="keyLength">密钥长度</param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public static byte[] GenerateFixedKey(string keyString, int keyLength)
        {
            // 确保密钥字符串的长度至少与所需的密钥长度一样长
            if (keyString.Length < keyLength)
            {
                throw new ArgumentException("Key string must be at least as long as the key length.");
            }

            // 使用密钥字符串的前keyLength个字符
            string fixedKey = keyString.Substring(0, keyLength);

            // 返回密钥字符串转换为UTF-8编码的字节数组
            return Encoding.UTF8.GetBytes(fixedKey);
        }

        public static string GenerateFixedKeyToBase64(string key)
        {
            string returnText = Convert.ToBase64String(GenerateFixedKey(key, 16));
            return returnText;
        }

        /// <summary>
        /// 例子
        /// </summary>
        public static void MainTest()
        {
            int keyLength = 16; // AES-128 requires a 16-byte key
            byte[] key = GenerateKey("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", keyLength);
            Console.WriteLine("Generated Key (in hex): " + BitConverter.ToString(key).Replace("-", ""));
        }

        /// <summary>
        /// 例子
        /// </summary>
        public static string MainTest2()
        {
            int keyLength = 16; // AES-128 requires a 16-byte key
            byte[] key = GenerateKey("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", keyLength);
            Console.WriteLine("Generated Key (in hex): " + BitConverter.ToString(key).Replace("-", ""));
            return "Generated Key (in hex): " + BitConverter.ToString(key).Replace("-", "");
        }
    }
}
